package com.lrkj.payfee.service.impl;

import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.payfee.domain.ChargeProject;
import com.lrkj.payfee.mapper.ChargeProjectMapper;
import com.lrkj.payfee.service.IChargeProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 费用类型Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-26
 */
@Service
public class ChargeProjectServiceImpl implements IChargeProjectService {
    @Autowired
    private ChargeProjectMapper chargeProjectMapper;

    /**
     * 查询费用类型
     *
     * @param projectId 费用类型ID
     * @return 费用类型
     */
    @Override
    public ChargeProject selectChargeProjectById(String projectId) {
        return chargeProjectMapper.selectChargeProjectById(projectId);
    }

    /**
     * 查询费用类型列表
     *
     * @param chargeProject 费用类型
     * @return 费用类型
     */
    @Override
    public List<ChargeProject> selectChargeProjectList(ChargeProject chargeProject) {
        return chargeProjectMapper.selectChargeProjectList(chargeProject);
    }

    /**
     * 新增费用类型
     *
     * @param chargeProject 费用类型
     * @return 结果
     */
    @Override
    public int insertChargeProject(ChargeProject chargeProject) {
        int rows = 0;
        try {
            chargeProject.setProjectId(chargeProject.getProjectCode());
            chargeProject.setCreateBy(SecurityUtils.getUsername());
            chargeProject.setCreateTime(DateUtils.getNowDate());
            rows = chargeProjectMapper.insertChargeProject(chargeProject);
        } catch (Exception e) {
            if(e.getMessage().indexOf("for key 'PRIMARY'") != -1){
                throw new BaseException("代码已存在！");
            }
        }
        return rows;
    }

    /**
     * 修改费用类型
     *
     * @param chargeProject 费用类型
     * @return 结果
     */
    @Override
    public int updateChargeProject(ChargeProject chargeProject) {
        chargeProject.setUpdateBy(SecurityUtils.getUsername());
        chargeProject.setUpdateTime(DateUtils.getNowDate());
        return chargeProjectMapper.updateChargeProject(chargeProject);
    }

    /**
     * 批量删除费用类型
     *
     * @param projectIds 需要删除的费用类型ID
     * @return 结果
     */
    @Override
    public int deleteChargeProjectByIds(String[] projectIds) {
        return chargeProjectMapper.deleteChargeProjectByIds(projectIds);
    }

    /**
     * 删除费用类型信息
     *
     * @param projectId 费用类型ID
     * @return 结果
     */
    @Override
    public int deleteChargeProjectById(String projectId) {
        return chargeProjectMapper.deleteChargeProjectById(projectId);
    }
}
